package com.his.controller;

import com.his.bean.DzmHisAuthRule;
import com.his.bean.DzmHisMember;
import com.his.config.JwtUtils;
import com.his.service.DzmHisAuthRuleService;
import com.his.service.DzmHisMemberService;
import com.his.service.DzmHisSupplierService;
import com.his.utils.ResponseResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dzm/his/member")
public class DzmHisMemberController {
//    @Autowired
//    DzmHisSupplierService ds;

    @Autowired
    DzmHisMemberService dzmHisMemberService;

    @Autowired
    DzmHisAuthRuleService dzmHisAuthRuleService;

    @PostMapping("/doLogin")
    public ResponseResult doLogin(String username, String password){
        System.out.println("11111111111111");
        Map map = new HashMap();
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
        }catch (AuthenticationException authenticationException){
            return new ResponseResult(500,"账户或者密码错误");
        }
        DzmHisMember dzmHisMember = (DzmHisMember)subject.getPrincipal();
        String jwt = JwtUtils.createJwt(username);
        map.put("msg","成功");
        map.put("data",dzmHisMember);
        map.put("jwt",jwt);
        return new ResponseResult(200,"成功",map);

    }

    //重置医生密码
    @PostMapping("updatePwd/{uid}")
    public ResponseResult updatePwd(@PathVariable String uid){
        int pwd = dzmHisMemberService.updatePwd(uid);
        if(pwd == 1){
            return new ResponseResult(200,"修改成功",null);
        }
        return new ResponseResult(500,"修改失败",null);

    }

    //禁用医生登录
    @PostMapping("jinyong/{uid}")
    public ResponseResult jinyong(@PathVariable String uid){
        int pwd = dzmHisMemberService.jinyong(uid);
        if(pwd == 1){
            return new ResponseResult(200,"修改成功",null);
        }
        return new ResponseResult(500,"修改失败",null);

    }

    //启用医生登录
    @PostMapping("qiyong/{uid}")
    public ResponseResult qiyong(@PathVariable String uid){
        int pwd = dzmHisMemberService.qiyong(uid);
        if(pwd == 1){
            return new ResponseResult(200,"修改成功",null);
        }
        return new ResponseResult(500,"修改失败",null);

    }


    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public ResponseResult getRouters(String uid) {
        List<String> listString = new ArrayList<>();
        int id = Integer.parseInt(uid);
        DzmHisMember dzmHisMember = dzmHisMemberService.memberUid(id);
        System.out.println(dzmHisMember);
        listString.add("admin");
        Map map = new HashMap<>();
        map.put("roles",listString);
        map.put("name",dzmHisMember.getUserName());
        map.put("routers",dzmHisMemberService.getRouters(uid));
        return new ResponseResult(200,"成功",map);
    }

}
